package DAO.MysqlDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import DAO.InterfaceDAO.LibraryDAO;
import Model.Library;

public class LibraryDAO_Mysql implements LibraryDAO {

	private PreparedStatement psCreate, psRead, psUpdate, psDelete, psFind;

	public LibraryDAO_Mysql(Connection cx) {

		try {
			psCreate = cx.prepareStatement("INSERT INTO library VALUES(?)");
			psRead   = cx.prepareStatement("SELECT * FROM library WHERE nom = ?");
			//psUpdate = cx.prepareStatement("UPDATE  SET  WHERE ");
			psDelete = cx.prepareStatement("DELETE FROM library WHERE nom = ?");
			psFind   = cx.prepareStatement("SELECT * FROM library");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean create(Library a) {
		try{
			psCreate.setString(1, a.getNom());
			psCreate.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public Library read(String id) {
		try {
			psRead.setString(1, id);
			ResultSet rs = psRead.executeQuery();
			rs.next();
			return new Library(rs.getString("nom"));			
		} catch (SQLException e) {
			e.printStackTrace();
		}	
		return null;
	}

	@Override
	public boolean update(Library a) {
		return false;
	}

	@Override
	public boolean delete(Library a) {
		try {
			psDelete.setString(1, a.getNom());
			psDelete.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

}
